<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RegExp</title>
</head>
<body>
    <script>
        'use strict';
        // 1.RegExp使用
        var re1_1 = /ABC\-001/;
        var re1_2 = new RegExp('ABC\\-001');
        console.log(re1_1);
        console.log(re1_2);

        var re = /^\d{3}\-\d{3,8}$/;
        console.log(re.test('010-12345')); // true
        console.log(re.test('010-1234x')); // false
        console.log(re.test('010 12345')); // false

        // 2.切分字符串
        // 用正则表达式切分字符串比用固定的字符更灵活，请看正常的切分代码:
        'a b   c'.split(' '); // ['a', 'b', '', '', 'c']

        // 正则表达式
        'a b   c'.split(/\s+/); // ['a', 'b', 'c']

        'a,b, c  d'.split(/[\s\,]+/); // ['a', 'b', 'c', 'd']

        'a,b;; c  d'.split(/[\s\,\;]+/); // ['a', 'b', 'c', 'd']

        // 3.分组
        var re2_1 = /^(\d{3})-(\d{3,8})/;
        console.log(re2_1.exec('010-12345')); // ['010-12345', '010', '12345']
        console.log(re2_1.exec('010 12345')); // null

        var re = /^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])$/;
        console.log(re.exec('19:05:30')); // ['19:05:30', '19', '05', '30']

        // 4.全局搜索
        //   JavaScript的正则表达式还有几个特殊的标志，最常用的是g，表示全局匹配：
        var r1 = /test/g;
        // 等价于:
        var r2 = new RegExp('test', 'g');

        //   全局匹配可以多次执行exec()方法来搜索一个匹配的字符串。当我们指定g标志后，每次运行exec()，
        //   正则表达式本身会更新lastIndex属性，表示上次匹配到的最后索引
        var s = 'JavaScript, VBScript, JScript and ECMAScript';
        var re=/[a-zA-Z]+Script/g;

        // 使用全局匹配:
        re.exec(s); // ['JavaScript']
        re.lastIndex; // 10

        re.exec(s); // ['VBScript']
        re.lastIndex; // 20

        re.exec(s); // ['JScript']
        re.lastIndex; // 29

        re.exec(s); // ['ECMAScript']
        re.lastIndex; // 44

        re.exec(s); // null，直到结束仍没有匹配到

        // 练习
        
        // 测试 1:
        var re_t1 = /^[\w+\.]+\@\w+\.(com|org)$/;

        var
            i,
            success = true,
            should_pass = ['someone@gmail.com', 'bill.gates@microsoft.com', 'tom@voyager.org', 'bob2015@163.com'],
            should_fail = ['test#gmail.com', 'bill@microsoft', 'bill%gates@ms.com', '@voyager.org'];
        for (i = 0; i < should_pass.length; i++) {
            if (!re_t1.test(should_pass[i])) {
                console.log('测试失败: ' + should_pass[i]);
                success = false;
                break;
            }
        }
        for (i = 0; i < should_fail.length; i++) {
            if (re_t1.test(should_fail[i])) {
                console.log('测试失败: ' + should_fail[i]);
                success = false;
                break;
            }
        }
        if (success) {
            console.log('测试通过!');
        }


        // 测试 2:
        var re_t2 = /^\<([\w\s\w]+)\>+\s+(\w+\@\w+\.org|com)$/;

        var r = re_t2.exec('<Tom Paris> tom@voyager.org');
        if (r === null || r.toString() !== ['<Tom Paris> tom@voyager.org', 'Tom Paris', 'tom@voyager.org'].toString()) {
            console.log('测试失败!');
        }
        else {
            console.log('测试成功!');
        }
    </script>
</body>
</html>